
#include <IRremote.h>

#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>

#include <pins_arduino.h>

//#include <Tone.h>
#include "SoftTone.h"


const int ledStatusPin = 13;
const int tonePin      = 12;

const int irDetectBPin = 7;
const int irDetectAPin = 6;

const int irEnableBPin = 5; 
const int irEnableAPin = 4; 
const int irOutPin     = 3; // must be pin 3 (Timer out)
//const int irDetectPin  = 8; // can be any pin


IRsend irsend;
//Tone tone1;

void setup()
{
  pinMode( ledStatusPin, OUTPUT);
  pinMode( tonePin,      OUTPUT);

  pinMode( irOutPin,     OUTPUT);
  pinMode( irEnableAPin, OUTPUT);
  pinMode( irEnableBPin, OUTPUT);
  digitalWrite( irEnableAPin, LOW ); // turn off LEDA
  digitalWrite( irEnableBPin, LOW ); // turn off LEDB

  pinMode( irDetectAPin,  INPUT);
  pinMode( irDetectBPin,  INPUT);
  digitalWrite(irDetectAPin, HIGH); // internal pullup
  digitalWrite(irDetectBPin, HIGH); // internal pullup
  
  irsend.enableIROut(38);
  irsend.mark(0);  // on

  /*
  tone1.begin(tonePin);
  tone1.play(NOTE_C3);
  delay(200);
  tone1.play(NOTE_C4);
  delay(200);
  tone1.play(NOTE_G3);
  delay(200);
  */

  Serial.begin(19200);
  Serial.println("BeamBreak2");

  delay(200);
  playTone( tonePin, NOTE_C,  100 );
  playTone( tonePin, NOTE_C1, 100 );
  playTone( tonePin, NOTE_G,  100 );
  

}

void loop()
{
  loop3();
}

unsigned long checkDetectPin(int pin)
{
  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  uint8_t stateMask = (HIGH ? bit : 0);

  unsigned long c = 0;
  if( (*portInputRegister(port) & bit) == stateMask ) {
    while ((*portInputRegister(port) & bit) == stateMask) {
      c++;
    }
    return clockCyclesToMicroseconds(c * 21 + 16);
  }
  return 0;
}

void loop5()
{

  digitalWrite( irEnableAPin, HIGH ); // turn on LEDA
  digitalWrite( ledStatusPin, HIGH);

  //int val = pulseIn( irDetectAPin, HIGH, 1000 );
  int val = checkDetectPin( irDetectAPin );
  if( val ) {
    Serial.print( val ); Serial.print(':');
    Serial.println( val/1000 );
  }

  digitalWrite(ledStatusPin, LOW);
  //if( val ) {
  //  Serial.println(val);
  // }

}

void loop4()
{

  digitalWrite( irEnableAPin, HIGH ); // turn on LEDA
  // attach a PinChange Interrupt to our pin on the rising edge
  PCintPort::attachInterrupt(irDetectAPin, detectA, RISING); 
  Serial.println("led A on");
  delay(3000);

  digitalWrite( irEnableAPin, LOW ); // turn off LEDA
  PCintPort::detachInterrupt(irDetectAPin);
  Serial.println("led A off");
  delay(3000);

}

volatile long detectStart;
volatile long detectEnd;
void detectA()
{
  //Serial.println("detectA!");
  //detectStart = millis();
  PCintPort::attachInterrupt(irDetectAPin, detectAend, FALLING); 
}
void detectAend()
{
  detectEnd = millis();
  PCintPort::detachInterrupt(irDetectAPin);
  
}

void loop3()
{

  //irsend.enableIROut(38);
  //irsend.mark(0);  // on

  digitalWrite( irEnableAPin, HIGH ); // turn on LEDA

  int v = digitalRead(irDetectAPin);
  digitalWrite(ledStatusPin, v);

  if( v == 1 ) { // detected
    long start = millis();
    while( v == 1 && ((millis()-start) < 1000) ) { 
      v = digitalRead(irDetectAPin);
    }
    playTone( tonePin, NOTE_C, 50 );
    long dur = millis()- start;
    Serial.println( dur );
  }

  digitalWrite( irEnableAPin, LOW ); // turn off LEDA

  //irsend.space(0); // off

}



